package cn.wolfcode.luowowo.wesite.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by LENOVO on 2019/8/10.
 */
@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IRgionService rgionService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public Object index(Model model){
        //所有的热门目的地和国内的
        //hotRegions
        List<Region> list = rgionService.queryHotRegion();
        model.addAttribute("hotRegions", list);
        return "destination/index";
    }
    //国内的专门用的
    @RequestMapping("getHotDestByRegionId")
    public String getHotDestByRegionId(Long regionId,Model model){
        //查询国内和热门地区的关联目的地
        List<Destination> list = destinationService.getDestByRegionId(regionId);
        //拆分数据
        int size = list.size();
        //左边的数据
        model.addAttribute("leftDests",list.subList(0,size/2+1));
        //右边的数据
        model.addAttribute("rightDests",list.subList(size/2+1,size));

        //配给模板
        model.addAttribute("regionId",regionId);

        return "destination/hotdestTpl";
    }


    //跳转
    @RequestMapping("guide")
    public  String guide(Long id,Model model){
        List<Destination> toasts = destinationService.getToasts(id);
        //查询到最后一个的时候减去那个目的地
        Destination dest = toasts.remove(toasts.size() - 1);

        model.addAttribute("toasts", toasts);

        //dest
        model.addAttribute("dest", dest);


        //攻略分类,同时关联查询攻略下面的明细 在概况下面出面
        model.addAttribute("catalogs",strategyCatalogService.queryCatalogByDestId(id));

        //查询前三篇
        List<StrategyDetail> strategyDetails = strategyDetailService.getViewNumTop3(id);
        model.addAttribute("strategyDetails",strategyDetails);

        return "destination/guide";
    }

    //目的地查询攻略
    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){

        List<Destination> toasts = destinationService.getToasts(qo.getDestId());//目的地的迭代
       //中国 广东
        Destination destination = toasts.remove(toasts.size() - 1);

        model.addAttribute("toasts",toasts);

        model.addAttribute("dest",destination);

        return "destination/survey";
    }
        //
    //查询攻略明细
    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        //qo接收了 传过来的地点id 和 攻略分类的id



        //catalogs 用目的地的id,查询到所有的攻略分类
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);

        //选中的分类 选中的攻略明细用id去查
        StrategyCatalog catalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog",catalog);

        //detail  选中的分类的第一个明细
        List<StrategyDetail> details = catalog.getDetails();
        if (details !=null && details.size()>0){
            StrategyDetail detail = details.get(0);
            detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
            model.addAttribute("detail",detail);
        }

        return "destination/surveyTpl";
    }
    //目的地下面的游记查询
    @RequestMapping("/travels")
    private String travels(Model model,@ModelAttribute("qo") TravelQuery qo){

        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "destination/travelTpl";
    }


}
